package com.zhuang.singleton.type7;

/**
 * @Classname SingletonTest07
 * @Description  静态内部类实现单例模式！
 * @Date 2021/3/17 9:59
 * @Created by dell
 */

public class SingletonTest07 {
    public static void main(String[] args) {
        System.out.println("静态内部类实现单例模式");
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        //判断是否为单例
        System.out.println(instance == instance2);
        System.out.println("intstance的哈希值" + instance.hashCode());
        System.out.println("intstance2的哈希值" + instance2.hashCode());
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton() {

    }

    //写一个静态内部类，该类中有个静态属性，Singleton
    public static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }

    //提供一个静态的公有方法，直接返回SingletonInstance.INSTANCE;
    public static synchronized Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}
